package com.gzstv.dlnasdk;

import android.app.Dialog;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ProgressBar;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.gzstv.dlnasdk.upnp.Device;

import java.util.ArrayList;
import java.util.List;

public class DeviceSelectionDialogFragment extends DialogFragment implements DLNAManager.DeviceDiscoveryListener {

    private RecyclerView deviceRecyclerView;
    private DeviceAdapter deviceAdapter;
    private ProgressBar progressBar;
    private TextView searchingTextView;

    public interface OnDeviceSelectedListener {
        void onDeviceSelected(Device device);
    }

    private OnDeviceSelectedListener listener;

    public void setOnDeviceSelectedListener(OnDeviceSelectedListener listener) {
        this.listener = listener;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.dialog_device_list, container, false);

        deviceRecyclerView = view.findViewById(R.id.deviceRecyclerView);
        progressBar = view.findViewById(R.id.progressBar);
        searchingTextView = view.findViewById(R.id.searchingTextView);

        deviceRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        deviceAdapter = new DeviceAdapter();
        deviceAdapter.setOnDeviceClickListener(device -> {
            if (listener != null) {
                listener.onDeviceSelected(device);
            }
            dismiss(); // Dismiss the dialog after selection
        });
        deviceRecyclerView.setAdapter(deviceAdapter);

        return view;
    }

    @Override
    public void onStart() {
        super.onStart();
        Dialog dialog = getDialog();
        if (dialog != null) {
            int width = ViewGroup.LayoutParams.MATCH_PARENT;
            int height = ViewGroup.LayoutParams.WRAP_CONTENT;
            dialog.getWindow().setLayout(width, height);
        }
        DLNAManager.getInstance().registerDeviceDiscoveryListener(this);
        DLNAManager.getInstance().startSearch();
        updateUI(true); // Show searching state initially
    }

    @Override
    public void onStop() {
        super.onStop();
        DLNAManager.getInstance().unregisterDeviceDiscoveryListener(this);
        DLNAManager.getInstance().stopSearch();
    }

    private void updateUI(boolean searching) {
        if (searching) {
            progressBar.setVisibility(View.VISIBLE);
            searchingTextView.setVisibility(View.VISIBLE);
            deviceRecyclerView.setVisibility(View.GONE);
        } else {
            progressBar.setVisibility(View.GONE);
            searchingTextView.setVisibility(View.GONE);
            deviceRecyclerView.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void onDeviceDiscovered(Device device) {
        if (getActivity() != null) {
            getActivity().runOnUiThread(() -> {
                deviceAdapter.addDevice(device);
                updateUI(false); // Hide searching state once devices are found
            });
        }
    }

    @Override
    public void onDeviceRemoved(Device device) {
        // TODO: Implement device removal from adapter
    }

    @Override
    public void onDiscoveryFinished() {
        if (getActivity() != null) {
            getActivity().runOnUiThread(() -> {
                updateUI(false); // Hide searching state when discovery finishes
            });
        }
    }
}